<?php

namespace App\Admin\Controllers;

use App\Http\Traits\GlobalConstant;
use App\Models\Campus;
use App\Models\Category;
use App\Models\Course;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class CampusController extends BaseController
{
    const   INVITE_STATUS = [
        'on'  => ['value' => 0, 'text' => '正常', 'color' => 'danger'],
        'off' => ['value' => 1, 'text' => '禁用', 'color' => 'default'],
    ];

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('校区列表')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('校区详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑校区')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增校区')
            ->description('新增校区')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Campus());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 3, function ($filter) {
                $filter->like('name', '校区名称');

            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('status', '状态')->select(['禁用', '正常']);
            });
            $filter->column(1 / 3, function ($filter) {
            });
        });
//
        $grid->model()->orderBy('id', 'desc');
        $grid->column('id', 'ID')->sortable();
        $grid->column('name', '校区名称')->limit(20);
        $grid->column('logo','logo')->lightbox(['width' => 30, 'height' => 30]);
        $grid->column('line_pics', '导航图片')->lightbox(['width' => 30, 'height' => 30]);
        $grid->column('room_pics', '教室图片')->lightbox(['width' => 30, 'height' => 30]);

        $grid->column('address', '地址');
        $grid->column('aa','打卡坐标')->display(function (){
            return $this->longitude.','.$this->latitude;
        });
        $grid->column('pouch_distance','打卡距离(米)');
        $grid->column('status', __('状态'))->display(function ($v) {
            return GlobalConstant::_STATUS[$v];
        })->label([0 => 'danger', 1 => 'primary']);
        $grid->column('created_at', __('created_at'));
        $grid->column('updated_at', __('updated_at'));

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableView();

        });
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });
        $grid->expandFilter();
        Admin::style('.img-thumbnail {width: 60px;height:60px}');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Campus::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new Campus());
        $form->text('name', '名称')->required();
        $form->text('contact', '联系电话')->required();
        $form->image('logo', 'logo')->required()->uniqueName();
        $form->multipleImage('line_pics', '导航图片')->uniqueName()->removable();
        $form->multipleImage('room_pics', '教室图片')->uniqueName()->removable();
        $form->text('address', '地址');
        $form->radio('status', '状态')->options([0 => '禁用', 1 => '正常']);
        $script = <<<SCRIPT

        document.onkeydown = function (event) {
            var eve = event ? event : (window.event ? window.event : null);
            if (eve.keyCode == 13) {
                return false;
            }
        };

                var lat = $('#latitude');
                var lng = $('#longitude');
                var center = new qq.maps.LatLng(lat.val(), lng.val());
                var container = document.getElementById("map_latitudelongitude");
                var map = new qq.maps.Map(container, {
                    center: center,
                    zoom: 16
                });
                var cirle = new qq.maps.Circle({
                    center: center,
                    radius: 300,
                    strokeWeight:1,
                    map: map
                });

                function init(name) {

                var marker = new qq.maps.Marker({
                    position: center,
                    draggable: true,
                    map: map
                });

                if( ! lat.val() || ! lng.val()) {
                    var citylocation = new qq.maps.CityService({
                        complete : function(result){
                        map.setCenter(result.detail.latLng);
                        marker.setPosition(result.detail.latLng);
                    }
                    });

                    citylocation.searchLocalCity();
                }

                qq.maps.event.addListener(map, 'click', function(event) {
                    marker.setPosition(event.latLng);
                     var aa = parseInt($('#pouch_distance').val());
                     cirle.setCenter(new qq.maps.LatLng(event.latLng.lat,event.latLng.lng))
                     cirle.setRadius(aa)
                });

                qq.maps.event.addListener(marker, 'position_changed', function(event) {
                    var position = marker.getPosition();
                    lat.val(position.getLat());
                    lng.val(position.getLng());
                });

                var ap = new qq.maps.place.Autocomplete(document.getElementById("search-latitudelongitude"));

                var searchService = new qq.maps.SearchService({
                    map : map
                });

                qq.maps.event.addListener(ap, "confirm", function(res){
                    searchService.search(res.value);
                });
            }

//            init('latitudelongitude');
            $('#pouch_distance').blur(function(){
                var aa = parseInt($(this).val());
                cirle.setRadius(aa);
                if(aa>=300){
                    map.setZoom(15)
                }
            })

SCRIPT;
        Admin::script($script,true);
        $form->latlong('latitude', 'longitude', '打卡地点')->height(600);

//        $html =<<<html
//        <script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=NSLBZ-V7AC2-OAEUM-CTWLI-34ZQV-EZFLS"></script>
//        <div style="height: 500px;" id="container"></div>
//html;
//        $form->text('latitude');
//        $form->text('longitude');
//        $script1 = <<<SCRIPT
//            var lat = $('#latitude');
//            var lng = $('#longitude');
//            var center = new qq.maps.LatLng(lat.val(), lng.val());
//             var container = document.getElementById("container");
//                var map = new qq.maps.Map(container, {
//                    center: center,
//                    zoom: 16
//                });
//SCRIPT;
//        Admin::script($script1);
//        $form->html($html,'打卡地点');
        $form->number('pouch_distance','打卡范围(米)')->default(100)->min(0);
        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        return $form;
    }

}
